package com.ruoyi.channel.service.impl;

import com.ruoyi.basedata.domain.PayKind;
import com.ruoyi.basedata.domain.WalletKind;
import com.ruoyi.basedata.mapper.PayKindMapper;
import com.ruoyi.basedata.mapper.WalletKindMapper;
import com.ruoyi.channel.domain.ChannelWallet;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.domain.MpChannelMethod;
import com.ruoyi.channel.mapper.ChannelWalletMapper;
import com.ruoyi.channel.mapper.MpChannelMapper;
import com.ruoyi.channel.mapper.MpChannelMethodMapper;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.financial.domain.FinancialWallet;
import com.ruoyi.financial.mapper.FinancialWalletMapper;
import com.ruoyi.merchant.domain.MpMerchant;
import com.ruoyi.merchant.domain.MpMerchantMethod;
import com.ruoyi.merchant.domain.MpMerchantRSP;
import com.ruoyi.merchant.mapper.MpMerchantMapper;
import com.ruoyi.merchant.mapper.MpMerchantMethodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-06
 */
@Service
public class MpChannelServiceImpl implements IMpChannelService
{
	/** 渠道名称是否唯一的返回结果 */
	public final static String MPCHANNEL_NAME_UNIQUE = "0";
	public final static String MPCHANNEL_NAME_NOT_UNIQUE = "1";

	@Autowired
	private MpChannelMapper mpChannelMapper;
	@Autowired
	private ChannelWalletMapper channelWalletMapper;
	@Autowired
	private FinancialWalletMapper financialWalletMapper;
	@Autowired
	private MpChannelMethodMapper channelMethodMapper;
	@Autowired
	private MpMerchantMethodMapper merchantMethodMapper;
	@Autowired
	private PayKindMapper payKindMapper;
	@Autowired
	private WalletKindMapper walletKindMapper;
	@Autowired
	private MpMerchantMapper merchantMapper;

	/**
     * 查询信息
     * @param id ID
     * @return 信息
     */
    @Override
	public MpChannel selectMpChannelById(Integer id)
	{
	    return mpChannelMapper.selectMpChannelById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param mpChannel 信息
     * @return 集合
     */
	@Override
	public List<MpChannel> selectMpChannelList(MpChannel mpChannel)
	{
	    return mpChannelMapper.selectMpChannelList(mpChannel);
	}
	
    /**
     * 新增
     * 
     * @param mpChannel 信息
     * @return 结果
     */
	@Override
	public AjaxResult insertMpChannel(MpChannel mpChannel, BigDecimal payRate, BigDecimal merchantPayRate, BigDecimal payLimitLeft, BigDecimal payLimitRight,Long[] payIds) {
		// 支付限额左不能大于或者等于支付限额右
		int PayFlag = payLimitLeft.compareTo(payLimitRight);
		if (PayFlag == 1 ){
			return AjaxResult.error("添加失败！渠道支付方法-支付限额范围不正确！<br/> 支付限额左 > 支付限额右");
		}else if (PayFlag == 0) {
			return AjaxResult.error("添加失败！渠道支付方法-支付限额范围不正确！<br/> 支付限额左 = 支付限额右");
		}
		// 商户支付方法费率要大于渠道支付方法费率
		int compare = merchantPayRate.compareTo(payRate);
		if (compare == -1 ){
			return AjaxResult.error("商户的支付方法费率要大于渠道支付方法的费率");
		}else if (compare == 0) {
			return AjaxResult.error("商户的支付方法费率要大于渠道支付方法的费率");
		}
		// 商户代付要续费要大于渠道代付手续费！
		int flag = mpChannel.getWithdrawalFee().compareTo(mpChannel.getMchWithdrawalFee());
		if (flag == 1) {
			return AjaxResult.error("商户代付手续费要大于渠道代付手续费！");
		}

		int withdrawalLimtFlag = mpChannel.getWithdrawalLimtLeft().compareTo(mpChannel.getWithdrawalLimtRight());
		if (withdrawalLimtFlag == 1 || withdrawalLimtFlag == 0) {
			return AjaxResult.error("代付限制左要小于代付限制右！");
		}
		// 新增渠道
		mpChannelMapper.insertMpChannel(mpChannel);
		// 拿到刚刚新增的渠道id  查看当前渠道是否添加了该类型的钱包 无则添加
		List<MpChannel> mpChannelList = mpChannelMapper.selectMpChannelByName(mpChannel.getName());
		if (mpChannelList.size() > 0) {
			MpChannel mpChannelAdd = mpChannelList.get(0);
			ChannelWallet channelWallet = channelWalletMapper.selectChannelWalletByChannelId(mpChannelAdd.getId());
			if (StringUtils.isNull(channelWallet)) {
				ChannelWallet channelWalletAdd = new ChannelWallet();
				// 渠道标识
				channelWalletAdd.setChannelId(mpChannelAdd.getId());
				// 余额
				BigDecimal accountBalance = new BigDecimal("0.00");
				channelWalletAdd.setAccountBalance(accountBalance);
				// 待结算金额
				BigDecimal waitLiquidatedBalance = new BigDecimal("0.00");
				channelWalletAdd.setWaitLiquidatedBalance(waitLiquidatedBalance);
				// 保存
				channelWalletMapper.insertChannelWallet(channelWalletAdd);
			}

			// 查看当前渠道下是否添加了该类型的财务钱包  无则添加
			FinancialWallet financialWallet = financialWalletMapper.selectFinancialWalletByChannelId(mpChannelAdd.getId());
			if (StringUtils.isNull(financialWallet)) {
				FinancialWallet financialWalletAdd = new FinancialWallet();
				// 渠道标识
				financialWalletAdd.setChannelId(mpChannelAdd.getId());
				// 余额
				BigDecimal accountBalance = new BigDecimal("0.00");
				financialWalletAdd.setAccountBalance(accountBalance);
				// 待结算金额
				BigDecimal frozenBalance = new BigDecimal("0.00");
				financialWalletAdd.setFrozenBalance(frozenBalance);
				// 保存
				financialWalletMapper.insertFinancialWallet(financialWalletAdd);
			}
		}

		// 同时添加所有支付类型
		if (payIds.length > 0) {
			addChannelMethod(mpChannelList.get(0), payRate, payLimitLeft, payLimitRight,payIds);
		}

		// 拿到刚刚新增的渠道支付方法    同时为所有商户添加此渠道的所有支付方法
		MpChannelMethod channelMethodExist = new MpChannelMethod();
		channelMethodExist.setChannelId(mpChannelList.get(0).getId());
		List<MpChannelMethod> channelMethodExistList = channelMethodMapper.selectMpChannelMethodList(channelMethodExist);
		if (channelMethodExistList.size() > 0) {
			addMerchantMethod(mpChannelList.get(0), channelMethodExistList, merchantPayRate);
		}
		return AjaxResult.success("添加成功！");
	}




	/**
     * 修改
     * 
     * @param mpChannel 信息
     * @return 结果
     */
	@Override
	public AjaxResult updateMpChannel(MpChannel mpChannel)
	{
		// 更新渠道支付方法的渠道信息
		MpChannelMethod channelMethodExist = new MpChannelMethod();
		channelMethodExist.setChannelId(mpChannel.getId());
		List<MpChannelMethod> channelMethodList = channelMethodMapper.selectMpChannelMethodList(channelMethodExist);
		if (channelMethodList.size() > 0) {
			for (MpChannelMethod channelMethod : channelMethodList) {
				channelMethod.setChannelName(mpChannel.getName());
				channelMethod.setChannelCode(mpChannel.getCode());
				channelMethod.setChannelStatus(mpChannel.getStatus());
				channelMethod.setChannelWithdrawalLevel(mpChannel.getWithdrawalLevel());
				channelMethod.setChannelWithdrawalFee(mpChannel.getWithdrawalFee());
				channelMethod.setChannelMchWithdrawalFee(mpChannel.getMchWithdrawalFee());
				channelMethod.setChannelWithdrawalLimtLeft(mpChannel.getWithdrawalLimtLeft());
				channelMethod.setChannelWithdrawalLimtRight(mpChannel.getWithdrawalLimtRight());
				channelMethod.setChannelClassName(mpChannel.getClassName());
				channelMethod.setChannelReqUrl(mpChannel.getReqUrl());
				channelMethod.setChannelSecretKey(mpChannel.getSecretKey());
				channelMethod.setChannelWithdrawalInner(mpChannel.getWithdrawalInner());
				// 更新渠道支付方法数据
				channelMethodMapper.updateMpChannelMethod(channelMethod);
			}
		}
		// 更新商户支付方法的渠道信息
		MpMerchantMethod mpMerchantMethodExist = new MpMerchantMethod();
		mpMerchantMethodExist.setChannelId(mpChannel.getId());
		List<MpMerchantMethod> merchantMethodList = merchantMethodMapper.selectMpMerchantMethodList(mpMerchantMethodExist);
		if (merchantMethodList.size() > 0) {
			for (MpMerchantMethod merchantMethod : merchantMethodList) {
				merchantMethod.setChannelName(mpChannel.getName());
				merchantMethod.setChannelCode(mpChannel.getCode());
				merchantMethod.setChannelStatus(mpChannel.getStatus());
				merchantMethod.setChannelWithdrawalLevel(mpChannel.getWithdrawalLevel());
				merchantMethod.setChannelWithdrawalFee(mpChannel.getWithdrawalFee());
				merchantMethod.setChannelMchWithdrawalFee(mpChannel.getMchWithdrawalFee());
				merchantMethod.setChannelWithdrawalLimtLeft(mpChannel.getWithdrawalLimtLeft());
				merchantMethod.setChannelWithdrawalLimtRight(mpChannel.getWithdrawalLimtRight());
				merchantMethod.setChannelClassName(mpChannel.getClassName());
				merchantMethod.setChannelReqUrl(mpChannel.getReqUrl());
				merchantMethod.setChannelSecretKey(mpChannel.getSecretKey());
				merchantMethod.setChannelWithdrawalInner(mpChannel.getWithdrawalInner());
				// 更新商户支付方法的渠道信息
				merchantMethodMapper.updateMpMerchantMethod(merchantMethod);
			}
		}

		int flag = mpChannel.getWithdrawalFee().compareTo(mpChannel.getMchWithdrawalFee());
		if (flag == 1) {
			return AjaxResult.error("商户代付要续费要大于渠道代付手续费！");
		}

		int withdrawalLimtFlag = mpChannel.getWithdrawalLimtLeft().compareTo(mpChannel.getWithdrawalLimtRight());
		if (withdrawalLimtFlag == 1 || withdrawalLimtFlag == 0) {
			return AjaxResult.error("代付限制左要小于代付限制右！");
		}
		mpChannelMapper.updateMpChannel(mpChannel);
	    return AjaxResult.success("修改成功！");
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMpChannelByIds(String ids)
	{
		return mpChannelMapper.deleteMpChannelByIds(Convert.toStrArray(ids));
	}


	/**
	 * 修改渠道状态
	 *
	 * @param mpChannel 渠道
	 * @return			结果
	 */
	@Override
	public int changeStatus(MpChannel mpChannel) {
		// 查询渠道支付方法
		MpChannelMethod channelMethod = new MpChannelMethod();
		channelMethod.setChannelId(mpChannel.getId());
		List<MpChannelMethod> channelMethodListExist = channelMethodMapper.selectMpChannelMethodList(channelMethod);
		// 更新渠道支付方法的渠道状态
		if (channelMethodListExist.size() > 0) {
			for (MpChannelMethod channelMethodExist : channelMethodListExist) {
				channelMethodExist.setChannelStatus(mpChannel.getStatus());
				// 更新
				channelMethodMapper.updateMpChannelMethod(channelMethodExist);
			}
		}
		// 查询商户支付方法
		MpMerchantMethod merchantMethod = new MpMerchantMethod();
		merchantMethod.setChannelId(mpChannel.getId());
		List<MpMerchantMethod> merchantMethodListExist = merchantMethodMapper.selectMpMerchantMethodList(merchantMethod);
		// 更新商户支付方法的渠道状态
		if (merchantMethodListExist.size() > 0) {
			for (MpMerchantMethod merchantMethodExist : merchantMethodListExist) {
				merchantMethodExist.setChannelStatus(mpChannel.getStatus());
				// 更新
				merchantMethodMapper.updateMpMerchantMethod(merchantMethodExist);
			}
		}
		return mpChannelMapper.updateMpChannel(mpChannel);
	}


	/**
	 * 内扣状态修改
	 *
	 * @param channel
	 * @return
	 */
	@Override
	public int changeInnerStatus(MpChannel channel) {
		// 查询渠道支付方法
		MpChannelMethod channelMethod = new MpChannelMethod();
		channelMethod.setChannelId(channel.getId());
		List<MpChannelMethod> channelMethodListExist = channelMethodMapper.selectMpChannelMethodList(channelMethod);
		// 更新渠道支付方法的渠道状态
		if (channelMethodListExist.size() > 0) {
			for (MpChannelMethod channelMethodExist : channelMethodListExist) {
				channelMethodExist.setChannelWithdrawalInner(channel.getWithdrawalInner());
				// 更新
				channelMethodMapper.updateMpChannelMethod(channelMethodExist);
			}
		}
		// 查询商户支付方法
		MpMerchantMethod merchantMethod = new MpMerchantMethod();
		merchantMethod.setChannelId(channel.getId());
		List<MpMerchantMethod> merchantMethodListExist = merchantMethodMapper.selectMpMerchantMethodList(merchantMethod);
		// 更新商户支付方法的渠道状态
		if (merchantMethodListExist.size() > 0) {
			for (MpMerchantMethod merchantMethodExist : merchantMethodListExist) {
				merchantMethodExist.setChannelWithdrawalInner(channel.getWithdrawalInner());
				// 更新
				merchantMethodMapper.updateMpMerchantMethod(merchantMethodExist);
			}
		}
		return mpChannelMapper.updateMpChannel(channel);
	}

	/**
	 * 校验名字唯一性
	 *
	 * @param mpChannel 渠道信息
	 * @return 结果
	 */
	@Override
	public String checkChannelNameUnique(MpChannel mpChannel) {
		Long mpChannelId = StringUtils.isNull(mpChannel.getId()) ? -1L : mpChannel.getId();
		MpChannel info = mpChannelMapper.checkChannelNameUnique(mpChannel.getName());
		if (StringUtils.isNotNull(info) && info.getId().longValue() != mpChannelId.longValue()) {
			return MPCHANNEL_NAME_NOT_UNIQUE;
		}
		return MPCHANNEL_NAME_UNIQUE;
	}


	/**
	 * 校验类名唯一性
	 *
	 * @param mpChannel 渠道信息
	 * @return 结果
	 */
	@Override
	public String checkClassNameUnique(MpChannel mpChannel) {
		MpChannel info = mpChannelMapper.checkClassNameUnique(mpChannel.getClassName());
		if (StringUtils.isNotNull(info)) {
			return MPCHANNEL_NAME_NOT_UNIQUE;
		}
		return MPCHANNEL_NAME_UNIQUE;
	}



	/**
	 * 根据渠道类型名称查询
	 *
	 * @param channelName 名称
	 * @return 渠道
	 */
	@Override
	public List<MpChannel> selectMpChannelByName(String channelName) {
		return mpChannelMapper.selectMpChannelByName(channelName);
	}



	private void addChannelMethod(MpChannel channel, BigDecimal payRate, BigDecimal payLimitLeft, BigDecimal payLimitRight,Long[] payIds) {
		// 支付类型
		List<PayKind> payKindList = payKindMapper.selectPayKindListByIds(payIds);

		// 要添加的渠道支付方法集合
		for (PayKind payKindAdd : payKindList) {
			MpChannelMethod channelMethod = new MpChannelMethod();
			// 渠道支付方法 - 信息
			channelMethod.setStatus(1);
			channelMethod.setPayRate(payRate);
			channelMethod.setPayLimitLeft(payLimitLeft);
			channelMethod.setPayLimitRight(payLimitRight);
			// 渠道支付方法 - 渠道信息
			channelMethod.setChannelId(channel.getId());
			channelMethod.setChannelName(channel.getName());
			channelMethod.setChannelCode(channel.getCode());
			channelMethod.setChannelStatus(channel.getStatus());
			channelMethod.setChannelWithdrawalLevel(channel.getWithdrawalLevel());
			channelMethod.setChannelWithdrawalFee(channel.getWithdrawalFee());
			channelMethod.setChannelMchWithdrawalFee(channel.getMchWithdrawalFee());
			channelMethod.setChannelWithdrawalLimtLeft(channel.getWithdrawalLimtLeft());
			channelMethod.setChannelWithdrawalLimtRight(channel.getWithdrawalLimtRight());
			channelMethod.setChannelClassName(channel.getClassName());
			channelMethod.setChannelReqUrl(channel.getReqUrl());
			channelMethod.setChannelSecretKey(channel.getSecretKey());
			channelMethod.setChannelWithdrawalInner(channel.getWithdrawalInner());
			// 渠道支付方法 - 支付类型信息
			channelMethod.setPayKindId(payKindAdd.getId());
			channelMethod.setPayKindName(payKindAdd.getName());
			channelMethod.setPayKindCode(payKindAdd.getCode());
			// 渠道支付方法 - 钱包类型信息
			// 微信扫码
			if (payKindAdd.getId() == 1) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(1);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 微信wap|H5
			if (payKindAdd.getId() == 2) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(1);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 支付宝扫码
			if (payKindAdd.getId() == 3) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(2);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 支付宝wap|H5
			if (payKindAdd.getId() == 4) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(2);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 银联快捷
			if (payKindAdd.getId() == 5) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(4);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 银联扫码
			if (payKindAdd.getId() == 6) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(5);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 网银网关
			if (payKindAdd.getId() == 7) {
				WalletKind walletKindAdd = walletKindMapper.selectWalletKindById(3);
				channelMethod.setWalletKindId(walletKindAdd.getId());
				channelMethod.setWalletKindName(walletKindAdd.getName());
				channelMethod.setWalletKindCode(walletKindAdd.getCode());
			}
			// 添加渠道支付方法
			channelMethodMapper.insertMpChannelMethod(channelMethod);
		}
		
	}


	/**
	 * 所有商户添加新增的渠道支付方法
	 * @param channel  				  新增的渠道
	 * @param channelMethodExistList  新增的渠道支付方法集合
	 * @param merchantPayRate  		  商户支付方法费率
	 */
	private void addMerchantMethod(MpChannel channel, List<MpChannelMethod> channelMethodExistList, BigDecimal merchantPayRate) {
		// 查询所有商户
		MpMerchant merchant = new MpMerchant();
		List<MpMerchantRSP> merchantRSPList = merchantMapper.selectMpMerchantList(merchant);
		// 要添加的商户支付方法
		List<MpMerchantMethod> insertMerchantMethods = new ArrayList<>();
		if (merchantRSPList.size() > 0) {
			for (MpMerchantRSP mpMerchantRSP : merchantRSPList) {
				for (MpChannelMethod channelMethod : channelMethodExistList) {
					MpMerchantMethod merchantMethod = new MpMerchantMethod();
					// 商户支付方法 - 信息
					merchantMethod.setPayRate(merchantPayRate);
					merchantMethod.setLevel(10);
					merchantMethod.setStatus(1);

					// 商户支付方法 - 商户信息
					merchantMethod.setMerchantId(mpMerchantRSP.getId());
					merchantMethod.setMerchantName(mpMerchantRSP.getName());
					merchantMethod.setMerchantCode(mpMerchantRSP.getCode());
					merchantMethod.setMerchantStatus(mpMerchantRSP.getStatus());

					// 商户支付方法 - 渠道支付方法信息
					merchantMethod.setChannelMethodId(channelMethod.getId());
					merchantMethod.setChannelMethodStatus(channelMethod.getStatus());
					merchantMethod.setChannelMethodPayRate(channelMethod.getPayRate());
					merchantMethod.setChannelMethodPayLimitLeft(channelMethod.getPayLimitLeft());
					merchantMethod.setChannelMethodPayLimitRight(channelMethod.getPayLimitRight());

					// 商户支付方法 - 渠道信息
					merchantMethod.setChannelId(channel.getId());
					merchantMethod.setChannelName(channel.getName());
					merchantMethod.setChannelCode(channel.getCode());
					merchantMethod.setChannelStatus(channel.getStatus());
					merchantMethod.setChannelWithdrawalLevel(channel.getWithdrawalLevel());
					merchantMethod.setChannelWithdrawalFee(channel.getWithdrawalFee());
					merchantMethod.setChannelMchWithdrawalFee(channel.getMchWithdrawalFee());
					merchantMethod.setChannelWithdrawalLimtLeft(channel.getWithdrawalLimtLeft());
					merchantMethod.setChannelWithdrawalLimtRight(channel.getWithdrawalLimtRight());
					merchantMethod.setChannelClassName(channel.getClassName());
					merchantMethod.setChannelReqUrl(channel.getReqUrl());
					merchantMethod.setChannelSecretKey(channel.getSecretKey());
					merchantMethod.setChannelWithdrawalInner(channel.getWithdrawalInner());

					// 商户支付方法 - 支付类型信息
					PayKind payKind = payKindMapper.selectPayKindById(channelMethod.getPayKindId());
					merchantMethod.setPayKindId(payKind.getId());
					merchantMethod.setPayKindName(payKind.getName());
					merchantMethod.setPayKindCode(payKind.getCode());

					// 商户支付方法 - 钱包类型信息
					WalletKind walletKind = walletKindMapper.selectWalletKindById(channelMethod.getWalletKindId().intValue());
					merchantMethod.setWalletKindId(walletKind.getId());
					merchantMethod.setWalletKindName(walletKind.getName());
					merchantMethod.setWalletKindCode(walletKind.getCode());

					// 保存
					insertMerchantMethods.add(merchantMethod);
				}
			}
		}
		// 添加商户支付方法
		if (insertMerchantMethods.size() > 0) {
			merchantMethodMapper.insertMerchantMethodList(insertMerchantMethods);
		}
	}
}
